package com.cxf.windex.biz;

import com.cxf.windex.base.biz.BaseBiz;
import com.cxf.windex.base.common.ServiceException;
import com.cxf.windex.base.constant.UserConstant;
import com.cxf.windex.base.util.DateUtil;
import com.cxf.windex.base.util.IdUtil;
import com.cxf.windex.entity.BOrders;
import com.cxf.windex.entity.BOrdersTask;
import com.cxf.windex.entity.BResouces;
import com.cxf.windex.entity.BShop;
import com.cxf.windex.entity.vo.ResourcesPay;
import com.cxf.windex.mapper.BOrdersMapper;
import com.github.pagehelper.PageHelper;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Date;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.helper.DataUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

/**
 * @program: business_needs
 * @description: 订单Biz
 * @author: chenxf
 * @create: 2018-10-17 14:27
 **/
@Slf4j
@Service
public class BOrdersBiz extends BaseBiz<BOrdersMapper,BOrders> {
  @Autowired
  private  BOrdersTaskBiz ordersTaskBiz;
  @Autowired
  private BResourcesBiz resourcesBiz;
  @Autowired
  private BShopBiz shopBiz;
  /**
   * 订单列表
   *
   * @param limit 数量
   * @param offset 页码
   * @param orderStr  排序(升序或者降序)
   * @param status  状态 0未发布 1进行中
   * @param shopId  店铺ID
   * @return
   */
  public List<BOrders> list(int limit, int offset,  String orderStr,String status,Long shopId) {
    Example example = new Example(BOrders.class);
    Example.Criteria criteria = example.createCriteria();
    criteria.andEqualTo("shopId",  shopId );
    criteria.andEqualTo("status", status);
    example.setOrderByClause("crt_time " + orderStr);
    PageHelper.startPage(offset, limit);
    return selectByExample(example);
  }

    public List<BOrders> adminlist(int limit, int offset,  String orderStr,String status) {
        Example example = new Example(BOrders.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIsNull("shopId" );
        if(StringUtils.isNotBlank(status)){
            criteria.andEqualTo("status", status);
        }
        example.setOrderByClause("crt_time " + orderStr);
        PageHelper.startPage(offset, limit);
        return selectByExample(example);
    }

    public void start(String orderId) {
        BOrders orders=selectById(orderId);
        if(orders==null){
            throw new ServiceException("订单不存在");
        }
        if(orders.getShopId()!=null){
            throw new ServiceException("只能开启平台订单");
        }
        if(!"0".equals(orders.getStatus())){
            throw new ServiceException("只能开启未开始订单");
        }

        if(orders.getBTime().before(DateUtil.getByLocalDate(LocalDate.now().plusDays(1)))){
            throw new ServiceException("开始时间不能为今天");
        }
        orders.setStatus("1");

        if(StringUtils.isNotBlank(orders.getResourcesIds())){
            ordersTaskBiz.saveAdminTasks(orders);
        }
        updateSelectiveById(orders);

    }

    public void del(String orderId) {
        BOrders orders=selectById(orderId);
        if(orders==null){
            throw new ServiceException("订单不存在");
        }
        if(orders.getShopId()!=null){
            throw new ServiceException("只能删除平台订单");
        }
        if(!"0".equals(orders.getStatus())){
            throw new ServiceException("只能删除未开始订单");
        }
        deleteById(orderId);
    }
  /**
   * 获取订单信息
   * @param orderId
   * @return
   */
  public BOrders getInfo(String orderId) {
   BOrders orders=selectById(orderId);
   if(StringUtils.isNotBlank(orders.getResourcesIds())){
     orders.setResoucesList(resourcesBiz.idsList(orders.getResourcesIds()));
     List<ResourcesPay> payList=ordersTaskBiz.queryTaskMoney(orderId,orders.getResourcesIds());
     for(BResouces resouces:orders.getResoucesList()){
       //增加资源任务支付信息信息
      resouces.initTaskList(payList);
     }
   }
   return orders;
  }

  /**
   * 保存订单信息
   * @param entity
   * @return 订单ID
   */
  public String save(BOrders entity){
    if(entity.getId()==null|| StringUtils.isBlank(entity.getId())){
       entity.setId(UserConstant.ORDERS_ID_PREX+new IdUtil(5,1).nextId());
       if(entity.getBTime()==null){
         entity.setBTime(DateUtil.getByLocalDate(LocalDate.now().plusDays(1)));
       }
       if(entity.getResourcesIds().startsWith(",")){
         entity.setResourcesIds(entity.getResourcesIds().substring(1));
       }
      entity.setETime(DateUtil.getByLocalDate(DateUtil.getLocalDateByDate(entity.getBTime()).plusDays(entity.getDays().intValue())));

       BShop bShop=shopBiz.selectById(entity.getShopId());
       if(bShop!=null){
         entity.setShopWxopenid(bShop.getWxopenid());
       }
      insertSelective(entity);
    }else{
      updateSelectiveById(entity);
    }

    if(entity.getStatus().equals("1") && entity.getResourcesIds().length()>0){
      //生成订单Task
      ordersTaskBiz.saveTasks(entity);
    }
    return  entity.getId();
  }

  /**
   * 发布订单信息
   * @param id 订单ID
   */
  public void suc(String id,String zcCount){
    BOrders orders=selectById(id);

    if(StringUtils.isNotBlank(orders.getResourcesIds())){
      ordersTaskBiz.saveTasks(orders);
    }
    orders.setStatus("1");
    orders.setTotalPrice(new BigDecimal(zcCount));
    updateSelectiveById(orders);
  }
}
